package com.im;

import mapgenerator.*;
import com.graphics.ImageContainer;
import com.utils.FileOperation;
import com.utils.MapFileFilter;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
import org.netbeans.lib.awtextra.AbsoluteConstraints;
import org.netbeans.lib.awtextra.AbsoluteLayout;

/**
 * ImageManagerDialogBrowseFileSample.java
 *
 * Created on Feb 17, 2009, 8:10:36 PM
 *
 * @author Sajarwo Anggai
 */
public class IMDialogBrowseFile extends JDialog {

    private JLabel jLabelHeight;
    private JLabel jLabelValueHeight;
    private JLabel jLabelValueWidth;
    private JLabel jLabelWidth;
    private JPanel jPanel1;
    private JScrollPane jScrollPaneImagePreview;
    private JFileChooser jf;
    private Project project;
    private ImageContainer imageContainer;
    private IMPanelWidget pw;

    public IMDialogBrowseFile(Project project, ImageContainer imageContainer, IMPanelWidget pw) {
        this.project = project;
        this.imageContainer = imageContainer;
        this.pw = pw;
        initComponents();
    }

    private void initComponents() {

        jf = new JFileChooser();
        jScrollPaneImagePreview = new JScrollPane();
        jPanel1 = new JPanel();
        jLabelWidth = new JLabel();
        jLabelHeight = new JLabel();
        jLabelValueWidth = new JLabel();
        jLabelValueHeight = new JLabel();

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Browse Image File");
        setLocationByPlatform(true);
        setResizable(false);
        getContentPane().setLayout(new AbsoluteLayout());

        MapFileFilter mff = new MapFileFilter("gif", "File GIF Only");
        MapFileFilter mff1 = new MapFileFilter("png", "File PNG Only");

        jf.setFileHidingEnabled(false);
        jf.setAcceptAllFileFilterUsed(false);
        jf.setFileFilter(mff);
        jf.setFileFilter(mff1);
        jf.setFileFilter(new MapFileFilter(true));

        jf.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                jfActionPerformed(evt);
            }
        });
        jf.addPropertyChangeListener(new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent evt) {
                jfPropertyChange(evt);
            }
        });
        getContentPane().add(jf, new AbsoluteConstraints(0, 0, 450, 440));

        jPanel1.setLayout(new GridLayout(1, 0));
        jScrollPaneImagePreview.setViewportView(jPanel1);
        getContentPane().add(jScrollPaneImagePreview, new AbsoluteConstraints(450, 0, 330, 370));

        jLabelWidth.setText("Width");
        getContentPane().add(jLabelWidth, new AbsoluteConstraints(650, 380, -1, 20));

        jLabelHeight.setText("Height");
        getContentPane().add(jLabelHeight, new AbsoluteConstraints(650, 410, -1, -1));
        getContentPane().add(jLabelValueWidth, new AbsoluteConstraints(690, 380, 90, 20));
        getContentPane().add(jLabelValueHeight, new AbsoluteConstraints(690, 410, 90, 20));

        pack();
    }

    /**
     * Handler untuk tombol Open dan Cancel
     * @param evt
     */
    private void jfActionPerformed(ActionEvent evt) {
        String comm = evt.getActionCommand();
        if (JFileChooser.APPROVE_SELECTION.equals(comm)) {
            File file = (File) jf.getSelectedFile();
            if (file.exists() && jf.accept(file) && !project.getPath().equals("")) {
                FileOperation fo = new FileOperation();
                File copyTo = new File(project.getPath() + File.separator + "images" + File.separator + file.getName());

                int val = JOptionPane.showConfirmDialog(this, "Do you want to rename this image?", "Rename", JOptionPane.YES_NO_OPTION);
                if (val == 0) {
                    String message = (String) JOptionPane.showInputDialog(this, "Please rename it bellow", "Rename File", JOptionPane.OK_CANCEL_OPTION);
                    try {
                        if (message.equals("")) {
                            return;
                        }
                    } catch (NullPointerException e) {
                        return;
                    }
                    copyTo = new File(copyTo.getParent() + File.separator + message.trim() + "." + MapFileFilter.getExtension(file));
                }

                while (true) {
                    if (copyTo.exists()) {
                        String msg = "File can't copied because has been exist!\n" +
                                "Please rename it bellow";
                        String message = (String) JOptionPane.showInputDialog(this, msg, "Rename File", JOptionPane.OK_CANCEL_OPTION);
                        try {
                            if (message.equals("")) {
                                return;
                            }
                        } catch (NullPointerException e) {
                            return;
                        }
                        copyTo = new File(copyTo.getParent() + File.separator + message.trim() + "." + MapFileFilter.getExtension(file));
                    } else {
                        break;
                    }
                }
                fo.copyFile(file.getAbsolutePath(), copyTo.getPath());
                imageContainer.addImage(copyTo.getName());
                pw.reloadAll();
                this.dispose();
            }
        } else if (JFileChooser.CANCEL_SELECTION.equals(comm)) {
            this.dispose();
        }
    }

    /**
     * Handler untuk menampilkan preview image di panel sebelah kanan
     * @param evt
     */
    private void jfPropertyChange(PropertyChangeEvent evt) {
        String prop = evt.getPropertyName();
        if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop) && evt.getNewValue() != null) {
            File file = (File) evt.getNewValue();
            if (file.exists() && jf.accept(file)) {
                ImageIcon im = new ImageIcon(file.getAbsolutePath());
                Dimension dim = new Dimension(im.getIconWidth(), im.getIconHeight());
                jLabelValueWidth.setText(": " + dim.getWidth() + "px");
                jLabelValueHeight.setText(": " + dim.getHeight() + "px");
                jPanel1.removeAll();
                jPanel1.setMinimumSize(dim);
                jPanel1.add(new JLabel(im));
                jPanel1.revalidate();
            }
        }
    }
}
